package me.zhengjie.modules.power.utilVideo;

import com.sun.jna.Native;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
import me.zhengjie.modules.power.domain.Video;
import me.zhengjie.modules.power.service.VideoService;
import me.zhengjie.modules.power.utilVideo.hik.HCNetSDK;
import me.zhengjie.modules.power.utilVideo.hik.OsSelect;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
//@Component
public class DvrVideoManage {

//    private static VideoService cameraServiceStatic;
//
//    public DvrVideoManage(VideoService cameraService) {
//        this.cameraServiceStatic = cameraService;
//    }


//    @Resource
    private static HCNetSDK hCNetSDK;
    public static volatile Map<String, DvrVideoControl> dvrVideoControlMap = new ConcurrentHashMap<>();
    private final static ThreadPoolExecutor executor = ThreadPoolExecutorUtil.getPoll();

    @Resource
    private VideoService cameraService;

    private static VideoService cameraServiceStatic;

//    @PostConstruct
    public void init(){
        cameraServiceStatic = this.cameraService;
    }

    /**
     * 启动推流
     *
     * @param cameraId /
     */
    public static boolean startPreviewStream(String cameraId) throws InterruptedException {
        boolean flag = false;
        if (Objects.isNull(dvrVideoControlMap.get(cameraId))) {
            Video camera = cameraServiceStatic.findById(cameraId);
            if (Objects.nonNull(camera) && Objects.nonNull(camera.getId())) {
                DvrVideoControl dvrVideoControl = new DvrVideoControl(camera);
                dvrVideoControlMap.put(camera.getId(), dvrVideoControl);
                // 启用摄像头预览
                int startPreviewNum = dvrVideoControl.startPreview();
                if (startPreviewNum > -1){
                    flag = true;
                }else{
                    // 预览失败停止摄像头预览并清理
                    stopPreviewStream(cameraId);
                }
            }
        }else{
            flag = true;
        }
        return flag;
    }

    /**
     * 停止推流
     * @param cameraId /
     */
    public static void stopPreviewStream(String cameraId) {
        DvrVideoControl dvrVideoControl = dvrVideoControlMap.get(cameraId);
        if (dvrVideoControl != null) {
            // 异步清理摄像头
            executor.execute(dvrVideoControl::clear);
            dvrVideoControlMap.remove(cameraId);
        }
    }

    /**
     * 初始化海康SDK
     * @return HCNetSDK
     */
    public static HCNetSDK getHCNetSDK() {
        if (hCNetSDK == null) {
            synchronized (HCNetSDK.class) {
                String strDllPath = "";
                try {
                    if (OsSelect.isWindows()) {
                        //win系统加载库路径
                        strDllPath = "C:\\\\HCNetSDK\\HCNetSDK.dll";
                    } else if (OsSelect.isLinux()) {
                        //Linux系统加载库路径
                        strDllPath = "/home/hik/LinuxSDK/libhcnetsdk.so";
                    }
                    log.info(strDllPath);
                    hCNetSDK = Native.load(strDllPath, HCNetSDK.class);
                    hCNetSDK.NET_DVR_Init();
                } catch (RuntimeException ex) {
                    log.error("HCNetSDK加载失败,路径：{},详情：{}", strDllPath, ex.getMessage());
                }
            }
        }
        return hCNetSDK;
    }


}
